package com.example.pluginmanager.controller;

import com.example.pluginmanager.entity.Menu;
import com.example.pluginmanager.service.MenuService;
import com.common.request.PageParam;
import com.common.result.PageVO;
import com.common.result.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("/query/getMenuById")
    public ResponseVO getMenuById(@RequestParam String menuId) {
        ResponseVO success = ResponseVO.success(menuService.getMenuById(menuId));
        return success;
    }

    @PostMapping("/query/getMenuList")
    public ResponseVO getMenuList(@RequestBody PageParam<Menu> menu) {
        PageVO<Menu> menuByPage = menuService.getMenuByPage(menu);
        return ResponseVO.success(menuByPage);
    }

    /*
     * 返回树形菜单
     * */
    @GetMapping("/query/getAllMenuList")
    public ResponseVO getAllMenuList(@RequestParam(name = "status", required = false) Integer status) {
        List<Menu> menuTree = menuService.getAllMenuList(status);
        return ResponseVO.success(menuTree);
    }

    /*
     * 返回所有菜单项,供前端动态路由
     * */
    @GetMapping("/query/getAllMenuItem")
    public ResponseVO getAllMenuItem() {
        List<Menu> allMenuItem = menuService.getAllMenuItem();
        return ResponseVO.success(allMenuItem);
    }

    /*
    批量启用,禁用菜单
    * 路径后拼接参数
    * */
    @PostMapping("/update/disableMenuByIds")
    public ResponseVO disableMenuByIds(@RequestParam(name = "ids") String[] ids, @RequestParam(name = "status") String status) {
        Integer integer = menuService.disableOrEnableMenuByIds(ids, status);
        if (integer > 0) {
            return ResponseVO.success(integer);
        }
        return ResponseVO.error("更新失败");
    }

    @PutMapping("/update/updateMenu")
    public ResponseVO updateMenuById(@RequestBody Menu menu) {
        Integer integer = menuService.modifyMenuById(menu);
        return ResponseVO.success(integer);
    }

    @PostMapping("/add/addMenu")
    public ResponseVO addMenu(@RequestBody Menu menu) {
        Integer row = menuService.addMenu(menu);
        return ResponseVO.success(row);
    }

    @GetMapping("/query/getAllChildrenMenu")
    public ResponseVO getAllChildrenMenu(@RequestParam("id") String id) {
        List<Menu> allChildrenMenu = menuService.getAllChildrenMenu(id);
        return ResponseVO.success(allChildrenMenu);
    }
}
